﻿package fj.ui
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	import fj.events.FJPaginationEvent;
	
	public class FJPaginator extends MovieClip
	{
		private var _previous:MovieClip;
		private var _next:MovieClip;
		private var _item:MovieClip;
		private var _itemClass:Class;
		
		private var _arrayPaginas:Array;
		private var _intervalo:Number;
		private var _paginaSelected:uint;
		private var _paginas:uint;
		
		public function FJPaginator():void
		{
			_previous = getChildByName( "previous" ) as MovieClip;
			_next = getChildByName( "next" ) as MovieClip;
			_item = getChildByName( "item" ) as MovieClip;
			_arrayPaginas = new Array();
			_paginaSelected = 1;
			if( _item ){
				_itemClass = Object( _item ).constructor;
			}
			_item.visible = false;
			_intervalo = _item.width;
			if( _previous ){
				_previous.disabled = true;
				_previous.addEventListener( MouseEvent.CLICK, previousClick );
			}
			if( _next ){
				_next.disabled = true;
				_next.addEventListener( MouseEvent.CLICK, nextClick );
			}
		}
		public function set paginas( u:uint ):void
		{
			_paginas = u;
			var i:uint;
			if( _item ){
				for( i=1; i <= _arrayPaginas.length; i++ ){
					var oldItem:MovieClip = _arrayPaginas[i] as MovieClip;
					removeChild( oldItem );
				}
				_arrayPaginas = null;
				_arrayPaginas = new Array();
				for( i=1; i <= u; i++ ){
					var tempItem:MovieClip = new _itemClass() as MovieClip;
					tempItem.x = _item.x + i*_intervalo;
					tempItem.y = _item.y;
					tempItem.id = i;
					if( tempItem.pagina_txt ){
						tempItem.pagina_txt.text = i;
					}
					tempItem.addEventListener( MouseEvent.CLICK, itemClick );
					_arrayPaginas[i] = tempItem;
					addChild( tempItem );
					if( i == _paginaSelected ){
						tempItem.selected = true;
					}
				}
			}
			if( _previous ){
				if( _paginaSelected > 1 ){
					_previous.disabled = false;
				} else {
					_previous.disabled = true;
				}
			}
			if( _next ){
				_next.x = _item.x-_previous.x+_paginas*_intervalo+_item.x;
				if( _paginas>1 && _paginaSelected < _paginas ){
					_next.disabled = false;
				} else {
					_next.disabled = true;
				}
			}
		}
		public function get paginas():uint
		{
			return _paginas;
		}
		public function set selected( u:uint ):void
		{
			_paginaSelected = u;
			if( _item ){
				var tempItem:MovieClip;
				for( var i:uint = 1; i < _arrayPaginas.length; i++ ){
					tempItem = _arrayPaginas[i] as MovieClip;
					if( tempItem.selected ){
						tempItem.selected = false;
					}
				}
				tempItem = _arrayPaginas[u] as MovieClip;
				tempItem.selected = true;
			}
			if( _previous ){
				if( _paginaSelected > 1 ){
					if( _previous.disabled ){
						_previous.disabled = false;
					}
				} else {
					if( !_previous.disabled ){
						_previous.disabled = true;
					}
				}
			}
			if( _next ){
				if( _paginaSelected < _paginas ){
					if( _next.disabled ){
						_next.disabled = false;
					}
				} else {
					if( !_next.disabled ){
						_next.disabled = true;
					}
				}
			}
			var evento:FJPaginationEvent = new FJPaginationEvent( _paginaSelected );
			dispatchEvent( evento );
		}
		public function get selected ():uint
		{
			return _paginaSelected;
		}
		private function previousClick( e:MouseEvent ):void
		{
			selected -= 1;
		}
		private function nextClick( e:MouseEvent ):void
		{
			selected += 1;
		}
		private function itemClick( e:MouseEvent ):void
		{
			selected = e.currentTarget.id;
		}
	}
}